package com.roger.springcloudGreenwich.api;

import com.roger.springcloudGreenwich.bean.Commodity;
import com.roger.springcloudGreenwich.bean.Epidemic;
import com.roger.springcloudGreenwich.bean.EpidemicPara;
import com.roger.springcloudGreenwich.component.DateUtil;
import com.roger.springcloudGreenwich.service.CommodityService;
import com.roger.springcloudGreenwich.service.EpidemicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by admin on 2019/12/11.
 */
@RestController
public class CommodityController {
    @Autowired
    private CommodityService commodityService;

    @Autowired
    private EpidemicService epidemicService;

    @GetMapping(value = "/save")
    public Object save(){
        Commodity commodity = new Commodity();
        commodity.setSkuId("1501009008");
        commodity.setName("欧米伽");
        commodity.setCategory("102");
        commodity.setPrice(1800);
        commodity.setBrand("brand");

        commodityService.save(commodity);
        return "success";
    }

    @GetMapping(value = "/getAll")
    public Object getAll(){
        return commodityService.getAll();
    }

    @GetMapping(value = "/getByName")
    public Object getByName(){
        List<Commodity> list1 = commodityService.getByName("商品", "brand");
        return list1.toString();
    }

    @PostMapping(value = "/saveEpidemic")
    public Object saveEpidemic(@RequestBody EpidemicPara epidemicPara) throws Exception{
        Epidemic epidemic = new Epidemic();
        String uuid = UUID.randomUUID().toString();
        epidemic.setUuid(uuid);
        epidemic.setUserName(epidemicPara.getUserName());
        epidemic.setBodyTemperature(epidemicPara.getBodyTemperature());
        epidemic.setCough(Boolean.valueOf(epidemicPara.getCough()));
        Timestamp leftDate = DateUtil.date2TimeStamp(epidemicPara.getLeftDate(),"yyyy-MM-dd");
        epidemic.setLeftDate(leftDate);
        Timestamp arrivalDate = DateUtil.date2TimeStamp(epidemicPara.getArrivalDate(),"yyyy-MM-dd");
        epidemic.setArrivalDate(arrivalDate);
        epidemic.setCurrentStatus(epidemicPara.getCurrentStatus());
        epidemic.setLocation(epidemicPara.getLocation());
        epidemicService.save(epidemic);
        return "success";
    }

    @GetMapping(value = "/deleteEpidemic/{uuid}")
    public Object deleteEpidemic(@PathVariable String uuid) throws Exception{
        Epidemic epidemic = new Epidemic();
        epidemic.setUuid(uuid);

        epidemicService.delete(epidemic);
        return "delete success";
    }


    public Date strToDate(String strDate) throws Exception{
//给定模式(这里给定的模式须与给定日期字符串格式匹配)
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
        Date d2 = sdf2.parse(strDate);
        System.out.println(d2);
        return d2;
    }
}
